package com.sixmac.controller;

import com.sixmac.annotation.AuthUser;
import com.sixmac.common.base.BaseController;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.module.RoomRepair;
import com.sixmac.module.Tenant;
import com.sixmac.module.TenantRoom;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * Created by admin on 2018/9/4.
 */
@Controller
@RequestMapping("/repair")
@ResponseBody
public class RepairController extends BaseController {


    /**
     * @api {post} /repair/index 0.2提交报修信息
     * @apiName repair.index
     * @apiGroup repair
     * @apiParam {String} token token
     * @apiParam {String} roomId phone
     * @apiParam {String} content 文字内容
     * @apiParam {String} images 图片多个用,分隔
     */
    @AuthUser
    @RequestMapping("/index")
    public AjaxResult index(Tenant tenant,String phone, String content, String images) {
        if (tenant.getStatus() != 1) {
            return error("当前用户已冻结");
        }

        TenantRoom tenantRoom = Blade.create(TenantRoom.class).findFirstBy("tenantId = #{tenantId} and status = 1", getData("tenantId", tenant.getId()));

        if (tenantRoom == null) {
            return error("当前用户未入住");
        }

        RoomRepair repair = new RoomRepair();
        repair.setCreateDate(new Date());
        repair.setPhone(phone);
        repair.setStatus(1);
        repair.setRoomId(tenantRoom.getRoomId());
        repair.setTenantId(tenant.getId());
        repair.setLandlordId(tenantRoom.getLandlordId());
        repair.setContent(content);
        repair.setImages(images);
        boolean f = Blade.create(RoomRepair.class).save(repair);
        if (f)
            return success("报修提交成功");
        return error("报修提交失败");
    }

}
